package redissvc

import (
	"errors"
	"github.com/spf13/viper"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver"
	v1 "gluenet/pkg/log/v1"
)

func init() {
	depend.Provide(NewRedisConfig)
	depend.Provide(NewService)
}

type Service struct {
	logger   v1.Logger
	redisCfg *RedisConfig
}

func NewService(redisCfg *RedisConfig) *Service {
	return &Service{
		logger:   v1.NewLogger(""),
		redisCfg: redisCfg,
	}
}

const (
	keyRedisAddr     = driver.KeyDriverPerfix + `redis.addr`
	keyRedisPassword = driver.KeyDriverPerfix + `redis.password`
)

type RedisConfig struct {
	Addr     string
	Password string
}

func NewRedisConfig(vp *viper.Viper) (*RedisConfig, error) {
	addr := vp.GetString(keyRedisAddr)
	if addr == "" {
		return nil, errors.New("empty redis addr")
	}
	pass := vp.GetString(keyRedisPassword)
	if addr == "" {
		return nil, errors.New("empty redis password")
	}

	return &RedisConfig{
		Addr:     addr,
		Password: pass,
	}, nil
}
